package com.example.demo.service.impl;

import com.example.demo.dto.Permission;
import com.example.demo.dto.Role;
import com.example.demo.dto.User;
import com.example.demo.entity.SysUser;
import com.example.demo.mapper.SysUserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhanpengguo
 * @date 2019-07-03 16:09
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public User findByName(String name) {
        User user = sysUserMapper.findByName(name);
        Long userId = user.getId();
        List<Role> roles = sysUserMapper.findRoleByUserId(userId);
        roles.forEach((role) -> {
            Long roleId = role.getId();
            List<Permission> permissions = sysUserMapper.findPermissionByRoleId(roleId);
            role.setPermissions(permissions);
        });
        user.setRoles(roles);
        return user;
    }

    @Override
    public User findOneUser(String name) {
        return sysUserMapper.findByName(name);
    }
}
